package com.longge.wechat.controller.wx;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.longge.wechat.communication.Poster;
import com.longge.wechat.message.req.ReqImageMessage;
import com.longge.wechat.message.resp.Article;
import com.longge.wechat.message.resp.RespArticleMessage;
import com.longge.wechat.utils.ReadProperties;
import com.longge.wechat.utils.XmlUtil;

@Controller
public class ImageController {
	

	@RequestMapping(value = "/saveImage.do", method = RequestMethod.POST)
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String xmlText = (String) request.getAttribute("xmlText");
		String openId = (String) request.getAttribute("openId");
		String from = (String) request.getAttribute("from");
		XmlUtil xmlUtil = XmlUtil.getInstance();

		
		ReqImageMessage pim = (ReqImageMessage) xmlUtil.xml2bean(xmlText,
				ReqImageMessage.class);
		
		// 向用户回复图文消息
		String mainPath = ReadProperties.loadProperties().getProperty("domainName");
		String picUrl = mainPath;
		String url = mainPath;
		String rs = "上传照片已收到，如想参加活动获得意外的惊喜，请点击‘‘关于我们-完善资料’’完善自己的详细资料，方便以后联系您，向您邮递获奖照片";
		Article article = new Article("照片详情",rs, picUrl, url);
		List<Article> articles = new ArrayList<Article>();
		articles.add(article);
		
		RespArticleMessage message = new RespArticleMessage(openId, from);
		message.setArticles(articles);
		
		String xml = xmlUtil.bean2xml(message, null, null).replaceAll("com.longge.wechat.message.resp.Article", "item");
		Poster.post(response, xml);
	}
	
	@RequestMapping(value = "/virtualWall.do", method = RequestMethod.POST)
	protected void virtualWall(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String openId = (String) request.getAttribute("openId");
		String from = (String) request.getAttribute("from");
		XmlUtil xmlUtil = XmlUtil.getInstance();
		
		// 向用户回复图文消息
		String mainPath = ReadProperties.loadProperties().getProperty("domainName");
		String picUrl = mainPath + "resources/images/scenery.jpg";
		String url = mainPath + "imageList.do?openId=" + openId;
		Article article = new Article("点击查看实景墙","", picUrl, url);
		List<Article> articles = new ArrayList<Article>();
		articles.add(article);
		
		RespArticleMessage message = new RespArticleMessage(openId, from);
		message.setArticles(articles);
		
		String xml = xmlUtil.bean2xml(message, null, null).replaceAll("com.longge.wechat.message.resp.Article", "item");
		Poster.post(response, xml);
	}
}
